package com.qf.taobei.controller;

import com.qf.taobei.entity.RespBean;
import com.qf.taobei.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/cart")
@Api(tags = "购物车模块")
public class CartController {

    @Autowired
    private CartService cartService;

    @PostMapping("/addCartProduct")
    @ApiOperation("添加到购物车")
    public RespBean addCartProduct(HttpServletRequest request, String number){
        Integer productId = (Integer) request.getSession().getAttribute("pid");
        RespBean respBean = cartService.addCartProduct(request, productId, Integer.parseInt(number));
        return respBean;
    }

    @GetMapping("/getProductList")
    @ApiOperation("查询购物车中的商品信息")
    public RespBean getProductList(HttpServletRequest request){
        RespBean respBean = cartService.getProductList(request);
        return respBean;
    }

    @GetMapping("/getProductListByName")
    @ApiOperation("根据名字查询商品")
    public RespBean getProductListByName(HttpServletRequest request,String name){
        RespBean respBean = cartService.getProductListByName(request, name);
        return respBean;
    }

    @PostMapping("/deleteProductById")
    @ApiOperation("删除对应的商品")
    public RespBean deleteProductById(String productId){
        RespBean respBean = cartService.deleteProductById(Integer.parseInt(productId));
        return respBean;
    }

    @PostMapping("/addProduct")
    @ApiOperation("增加购物车中有的商品")
    public RespBean addProduct(String CPId){
        RespBean respBean = cartService.addProduct(Integer.parseInt(CPId));
        return respBean;
    }

    @PostMapping("/deleteProduct")
    @ApiOperation("删除对应的商品")
    public RespBean deleteProduct(int CPId){
        RespBean respBean = cartService.deleteProduct(CPId);
        return respBean;
    }

    @PostMapping("/deleteProductAll")
    @ApiOperation("多选删除")
    public RespBean deleteProductAll( @RequestParam(value = "list",required = true) Integer[] list){
        List<Integer> integers = Arrays.asList(list);
        RespBean respBean = cartService.deleteProductAll(integers);
        return respBean;
    }

    @GetMapping("/getMoneyAll")
    @ApiOperation("获取总价")
    public RespBean getMoneyAll(@RequestParam(value = "list",required = true) Integer[] list){
        List<Integer> integers = Arrays.asList(list);
        RespBean moneyAll = cartService.getMoneyAll(integers);
        return moneyAll;
    }

    @GetMapping("/getCartSum")
    @ApiOperation("查询购物车商品数量")
    public RespBean getCartSum(HttpServletRequest request){
        RespBean cartSum = cartService.getCartSum(request);
        return cartSum;
    }
    @GetMapping("/getProductID")
    @ApiOperation("结算")
    public RespBean getProductID(HttpServletRequest request,@RequestParam(value = "list",required = true) Integer[] list){
        RespBean respBean = cartService.getProductID(request, Arrays.asList(list));
        return respBean;
    }
}
